const express = require("express");
const app = express();
const config = require("../../config");
const { AuthMiddleware, CorsMiddleware, ResponseMiddleware, DownloadMiddleware } = require("./middleware");

const harRouter = require("./har");
const jmxRouter = require("./jmx");
const remoteRouter = require("./remote");
const executionRouter = require("./execution");

app.use(express.json());
app.use(new CorsMiddleware().middleware);
app.use(new ResponseMiddleware().middleware);
app.use(new DownloadMiddleware().middleware);
app.use("/data", express.static(config.path.data));

app.use("/api/har", new AuthMiddleware().middleware, harRouter);
app.use("/api/jmx", new AuthMiddleware().middleware, jmxRouter);
app.use("/api/remote", new AuthMiddleware().middleware, remoteRouter);
app.use("/api/execution", new AuthMiddleware().middleware, executionRouter);

const port = 8888;
app.listen(port, () => {
    console.log(`Server is running on port ${port}`);
});

// 清理不在线远程机
const { Registration } = require("../remoteMachine");
const registration = new Registration();
setInterval(async () => {
    await registration.clean();
}, 30000);
